package com.tgy.数组和集合转换;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/***
 * @ClassName: MyArrays
 * @Description: 测试数组
 * @Auther: 送你一块二向箔
 * @Date: 21:56 2022/5/1
 * @version : V1.0
 */
public class MyArrays {
    public static void main(String[] args) {
       // test();
        intTest();
    }

    public static void test(){
        //创建一个数组
        //int [] intArr = new int[10];
        int [] intArr1 = {1,2,1,2,1,2,12};
        String [] strArr = {"a","b","c","d","e","f","g"};
        //数组转集合
        List<String> strings = Arrays.asList(strArr);
        System.out.println(strings instanceof Collection);//true
        //集合转数组
        String[] strings1 = strings.toArray(new String[0]);
        System.out.println(strings1 instanceof String[]);//true
        Arrays.stream(strings1).forEach(System.out::print);

    }

    public static void intTest(){
        //创建一个int数组
        int [] intArr = {1,2,1,2,1,2,12};
        //int数组转集合
        List<Integer> collect = Arrays.stream(intArr).boxed().collect(Collectors.toList());
        System.out.println(collect instanceof Collection);//true
        //集合转数组
        int[] ints = collect.stream().mapToInt(Integer::intValue).toArray();
        for (int anInt : ints) {
            System.out.print(anInt+" ");
        }

        //这里注意Collotion只能存放对象，不能存放基本数据类型
        Integer[] integers = collect.toArray(new Integer[0]);
        Arrays.stream(integers).forEach((a)-> System.out.println("数字："+a));

    }

}
